<?php
/**
 * ProjectsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-04-12 16:06:12
 * @modified    2023-04-12 16:06:12
 *
 */

namespace App\Http\Controllers;

use App\Repositories\ProjectRepo;
use Illuminate\Http\Request;

class ProjectsController extends BaseModuleController
{
    /**
     * @OA\Get(
     *     path="/projects/params",
     *     tags={"Project"},
     *     summary="获取项目参数",
     *     description="获取项目参数",
     *     operationId="project_params",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function params(Request $request)
    {
        return message('获取成功', true, ProjectRepo::params());
    }

    /**
     * @OA\Get(
     *     path="/projects",
     *     tags={"Project"},
     *     summary="获取项目列表",
     *     description="获取项目列表",
     *     operationId="project_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="name", in="query", description="项目名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="project_number", in="query", description="项目编号", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="province_id", in="query", description="省份ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="city_id", in="query", description="城市ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="county_id", in="query", description="区县ID", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        try {
            $projects = ProjectRepo::getList();
            return list_message('获取成功', true, $projects);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/projects",
     *     tags={"Project"},
     *     summary="新建项目",
     *     description="新建项目",
     *     operationId="project_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "customer_id", "user_id", "device_count", "province_id", "city_id", "county_id", "address", "plan_start_at", "plan_complete_at", "plan_work_hour", "status"},
     *              @OA\Property(property="name", type="string", example="项目名称", description="项目名称"),
     *              @OA\Property(property="customer_id", type="integer", example="所属客户ID", description="所属客户ID"),
     *              @OA\Property(property="user_id", type="integer", example="负责人ID", description="负责人ID"),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="详细地址", description="详细地址"),
     *              @OA\Property(property="plan_start_at", type="string", example="计划开始时间", description="计划开始时间"),
     *              @OA\Property(property="plan_complete_at", type="string", example="计划完成时间", description="计划完成时间"),
     *              @OA\Property(property="plan_work_hour", type="number", example="计划工时, 新建时填写，修改时不填写", description="计划工时"),
     *              @OA\Property(property="status", type="string", example="状态，修改项目时必填，新修时不填写", description="计划工时"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $project = ProjectRepo::store();
            return message('创建成功', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/projects/{id}",
     *     tags={"Project"},
     *     summary="编辑项目",
     *     description="编辑项目",
     *     operationId="project_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="项目ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"name", "customer_id", "user_id", "device_count", "province_id", "city_id", "county_id", "address", "plan_start_at", "plan_complete_at", "plan_work_hour", "status"},
     *              @OA\Property(property="name", type="string", example="项目名称", description="项目名称"),
     *              @OA\Property(property="customer_id", type="integer", example="所属客户ID", description="所属客户ID"),
     *              @OA\Property(property="user_id", type="integer", example="负责人ID", description="负责人ID"),
     *              @OA\Property(property="province_id", type="integer", example="省份ID", description="省份ID"),
     *              @OA\Property(property="city_id", type="integer", example="城市ID", description="城市ID"),
     *              @OA\Property(property="county_id", type="integer", example="区县ID", description="区县ID"),
     *              @OA\Property(property="address", type="string", example="详细地址", description="详细地址"),
     *              @OA\Property(property="plan_start_at", type="string", example="计划开始时间", description="计划开始时间"),
     *              @OA\Property(property="plan_complete_at", type="string", example="计划完成时间", description="计划完成时间"),
     *              @OA\Property(property="status", type="string", example="状态", description="计划工时"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $project = ProjectRepo::store($id);
            return message('编辑成功', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/projects/{id}",
     *     tags={"Project"},
     *     summary="获取项目详情",
     *     description="获取项目详情",
     *     operationId="project_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="项目ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $project = ProjectRepo::fetch($id);
            return message('创建成功', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/projects/{id}/start",
     *     tags={"Project"},
     *     summary="开始项目",
     *     description="开始项目",
     *     operationId="project_start",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="项目ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"start_at"},
     *                 @OA\Property(property="start_at", description="实际开始时间(xxxx-xx-xx)", type="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function start(Request $request, $id)
    {
        try {
            $project = ProjectRepo::start($id);
            return message('开始成功', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function re_start(Request $request, $id)
    {
        try {
            $project = ProjectRepo::reStart($id);
            return message('重新开始成功', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/projects/{id}/close",
     *     tags={"Project"},
     *     summary="关闭项目",
     *     description="关闭项目",
     *     operationId="project_close",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="项目ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"closed_at"},
     *                 @OA\Property(property="closed_at", description="实际关闭时间(xxxx-xx-xx)", type="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function close(Request $request, $id)
    {
        try {
            ProjectRepo::close($id);
            return message('关闭成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/projects/{id}",
     *     tags={"Project"},
     *     summary="删除项目",
     *     description="删除项目",
     *     operationId="project_destory",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="项目ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            ProjectRepo::destroy($id);
            return message('删除成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function complete(Request $request, $id)
    {
        try {
            $project = ProjectRepo::complete($id);
            return message('成功完成项目', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function products(Request $request, $id)
    {
        try {
            ProjectRepo::products($id);
            return message('关联成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function products_list(Request $request, $id)
    {
        try {
            $products = ProjectRepo::product_list($id);
            return list_message('获取成功', true, $products);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function filing(Request $request, $id)
    {
        try {
            $project = ProjectRepo::filing($id);
            return message('成功归档项目', true, $project);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function work_day(Request $request, $id)
    {
        try {
            $data = ProjectRepo::work_day($id);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function total_task(Request $request, $id)
    {
        try {
            $data = ProjectRepo::total_task($id);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function task_complete_counts(Request $request, $id)
    {
        try {
            $data = ProjectRepo::taskCompleteCounts($id);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function schedule_total_task(Request $request, $id)
    {
        try {
            $data = ProjectRepo::schedule_total_task($id);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function gantt_projects(Request $request)
    {
        try {
            $data = ProjectRepo::gantt_projects();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function gantt_data()
    {
        try {
            $data = ProjectRepo::gantt_data();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function simple(Request $request)
    {
        try {
            return message('获取成功', true, ProjectRepo::simpleData());
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 燃尽图
    public function burn_down(Request $request, $id)
    {
        try {
            $data = ProjectRepo::burnDown($id);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function products_unbind(Request $request)
    {
        try {
            ProjectRepo::products_unbind();
            return message('解绑成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function projects_collect(Request $request)
    {
        try {
            $data = ProjectRepo::projects_collect();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function type_tasks(Request $request)
    {
        try {
            $data = ProjectRepo::typeTasks();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function over_view(Request $request)
    {
        try {
            $data = ProjectRepo::over_view();
            return list_message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}